﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Krist_em_up.Components;
using System.Runtime.Serialization.Formatters.Binary;

namespace Krist_em_up.Settings
{
    [Serializable]
    public class Settings
    {
        public ControlsConfig PlayerOne;
        public ControlsConfig PlayerTwo;

        public const string SETTINGS_FILE = "settings.cfg";

        public Settings(ControlsConfig ctrls1, ControlsConfig ctrls2)
        {
            PlayerOne = ctrls1;
            PlayerTwo = ctrls2;
        }

        public Settings()
        {
            PlayerOne = ControlsConfig.DefaultPlayerOne();
            PlayerTwo = ControlsConfig.DefaultPlayerTwo();
        }

        public void SaveIntoFile()
        {
            //if (File.Exists(SETTINGS_FILE))
            //{
                FileStream f = new FileStream(SETTINGS_FILE, FileMode.OpenOrCreate, FileAccess.Write);
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(f, this);
                f.Close();
            //}
        }

        public Settings FromFile()
        {
            if (File.Exists(SETTINGS_FILE))
            {
                FileStream f = new FileStream(SETTINGS_FILE, FileMode.Open, FileAccess.Read);
                BinaryFormatter formatter = new BinaryFormatter();
                Settings s = (Settings) formatter.Deserialize(f);
                f.Close();
                return s;
            }
            else
                return this;
        }
    }
}
